package io.adrainty.bolee.security.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.adrainty.bolee.framework.commons.utils.IdentifierNoUtil;
import io.adrainty.bolee.security.mapper.IPostMapper;
import io.adrainty.bolee.security.models.bo.PostBO;
import io.adrainty.bolee.security.models.entity.PostEntity;
import io.adrainty.bolee.security.repository.IPostRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/12 上午12:25
 */

@Repository
public class PostRepositoryImpl implements IPostRepository {

    @Resource
    private IPostMapper postMapper;

    @Override
    public IPage<PostEntity> pagePost(Integer pageNum, Integer pageSize, PostEntity entity) {
        Page<PostBO> page = Page.of(pageNum, pageSize);
        Wrapper<PostBO> wrapper = getWrapper(entity);
        return postMapper.selectPage(page, wrapper).convert(item -> BeanUtil.toBean(item, PostEntity.class));
    }

    @Override
    public boolean createPost(PostEntity entity) {
        PostBO postBO = BeanUtil.toBean(entity, PostBO.class);
        boolean result = postMapper.insert(postBO) > 0;
        if (!result) return false;
        BeanUtil.copyProperties(postBO, entity);
        return true;
    }

    @Override
    public boolean updatePost(PostEntity entity) {
        PostBO postBO = BeanUtil.toBean(entity, PostBO.class);
        return postMapper.updateById(postBO) > 0;
    }

    @Override
    public List<PostEntity> listPost(PostEntity entity) {
        Wrapper<PostBO> wrapper = getWrapper(entity);
        List<PostBO> postBOList = postMapper.selectList(wrapper);
        return BeanUtil.copyToList(postBOList, PostEntity.class);
    }

    @Override
    public List<PostEntity> getDeptPostList(String deptNo) {
        Wrapper<PostBO> wrapper = Wrappers.lambdaQuery(PostBO.class)
                .eq(PostBO::getDeptNo, deptNo);
        List<PostBO> postBOList = postMapper.selectList(wrapper);
        return BeanUtil.copyToList(postBOList, PostEntity.class);
    }

    private Wrapper<PostBO> getWrapper(PostEntity entity) {
        return Wrappers.lambdaQuery(PostBO.class)
                .eq(StringUtils.isNotBlank(entity.getPostNo()), PostBO::getPostNo, entity.getPostNo())
                .eq(entity.getDataState() != null, PostBO::getDataState,entity.getDataState())
                .likeRight(StringUtils.isNotBlank(entity.getPostName()), PostBO::getPostName, entity.getPostName())
                .likeRight(StringUtils.isNotBlank(entity.getDeptNo()), PostBO::getDeptNo,
                        StringUtils.defaultIfBlank(entity.getDeptNo(), IdentifierNoUtil.ROOT_PARENT_ID))
                .orderByAsc(PostBO::getSortNo);
    }

}
